/*
* Copyright 2024 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/


/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include "lvgl.h"
#include "scroll_bar.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**
 * Create a demo application
 */


/********************************************       application       **********************************************/

static void scroll_item_childs_create_cb(lv_obj_t * item, uint16_t index);
static void scroll_item_childs_scale_cb(lv_obj_t * item, float scale);

static struct scroll_bar_s scroll_bar = {
    .content = NULL,
    .child_nums = 7,
    .spacing = 100,
    .selected = 3,
    .direction = 0,
    .onesnap = 1,
    .scale_min = 0.7,
    .scale_max = 1.0,
    .item_childs_create_cb = scroll_item_childs_create_cb,
    .item_childs_scale_cb = scroll_item_childs_scale_cb,
    .item_size = {100,100},
};

static void scroll_item_childs_create_cb(lv_obj_t * item, uint16_t index)
{
    lv_obj_set_style_bg_color(item,lv_color_hex(lv_rand(0,0xffffff)),0);
    lv_obj_t * label = lv_label_create(item);
    lv_obj_center(item);
    lv_label_set_text_fmt(label,"L%d",index);
}

static void scroll_item_childs_scale_cb(lv_obj_t * item, float scale)
{
    lv_obj_t * label = lv_obj_get_child(item,0);
    lv_obj_set_style_transform_scale(label,scale*255,0);
}

static void content_scroll_cb(lv_event_t * e)
{
    struct scroll_bar_s * scroll_bar = lv_event_get_user_data(e);
    lv_obj_t * content = lv_event_get_target(e);
    lv_ui * ui = lv_obj_get_user_data(content);
    lv_label_set_text_fmt(ui->screen_label_1,"scroll_x:%d,scroll_y:%d,selected:%d",lv_obj_get_scroll_x(content) ,lv_obj_get_scroll_y(content),scroll_bar->selected);
}

void custom_init(lv_ui *ui)
{
    /* Add your codes here */
    
    scroll_bar_create(ui->screen_cont,&scroll_bar);
    scroll_bar.content->user_data = ui;
    lv_obj_add_event_cb(scroll_bar.content,content_scroll_cb,LV_EVENT_SCROLL,&scroll_bar);
}

